package tgc.edu.yzy.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import tgc.edu.yzy.entity.Admin;
import tgc.edu.yzy.entity.Bj;
import tgc.edu.yzy.entity.Grade;
import tgc.edu.yzy.entity.Student;
import tgc.edu.yzy.entity.Teacher;
import tgc.edu.yzy.service.AdminService;
import tgc.edu.yzy.service.BjService;
import tgc.edu.yzy.service.GradeService;
import tgc.edu.yzy.service.StudentService;
import tgc.edu.yzy.service.TeacherService;

@Controller
@RequestMapping(value="/admin")
public class AdminController {
	@Autowired
	private AdminService adminservice;
	@Autowired
	private TeacherService teacherservice;
	@Autowired
	private StudentService studentservice;
	@Autowired
	private BjService bjservice;
	@Autowired
	private GradeService gradeservice;
	/***
	 * 修改密码
	 * @return
	 */
	@RequestMapping(value="xgpassword")
	public String xgpassword(String password,HttpSession session,ModelMap map) {
		Admin admins=(Admin) session.getAttribute("admins");
		adminservice.updatepassword(password,admins.getId());
		map.put("password", "管理员");
		session.removeAttribute("admins");
		return "homepage/index";
	}
	//
	//=====================对课程（任课教师）表进行操作=================================================
	//
	/***
	 * 显示所有老师
	 * @param map
	 * @param page
	 * @return
	 */
	@RequestMapping(value="teacher")
	public String teacher(ModelMap map,Integer page,HttpSession session) {
		if(page==null) {
			page=0;
		}
		Pageable pageable=PageRequest.of(page, 10);
		Page<Teacher> teacherpage=teacherservice.findAll(pageable);
		String sql = "<ui class='pagination pagination-sm'>";
		int a5=teacherpage.getTotalPages();
		for (int i = 0; i < a5; i++) {
			sql+="<li><a  href='teacher?page="+i+"'>"+(i+1)+"</a></li>";
		}
		sql=sql+"</ui>";
		map.put("sql", sql);
		map.put("page", teacherpage.getContent());
		map.put("xianshi","第"+(teacherpage.getNumber()+1)+"页&emsp;显示数据"+teacherpage.getNumberOfElements()+"条&emsp;&emsp;总共"+teacherpage.getTotalPages()+"页&emsp;共"+teacherpage.getTotalElements()+"条数据" );
		String hint = (String) session.getAttribute("yao");
		session.removeAttribute("yao");
		map.put("hint", hint);
		String update=(String) session.getAttribute("update");
		session.removeAttribute("update");
		map.put("update", update);
		return "admin/teacher";
	}
	/***
	 * 重置教师密码
	 * @param ec
	 * @param teacherid
	 * @param session
	 * @return
	 */
	@RequestMapping(value="updatepassword",method=RequestMethod.POST)
	public String updatepassword(String ec,Integer teacherid,HttpSession session) {
		String password="123";
		teacherservice.updatepassword(password, teacherid);
		session.setAttribute("yao", "教师："+ec+"  密码重置成功！");
		return "redirect:teacher";
	}
	/***
	 * 删除teacher表（老师）
	 * @param ec	传过来的老师姓名
	 * @param teacherid   老师所对应的ID好
	 * @param session 
	 * @return
	 */
	@RequestMapping(value="selectstudent",method=RequestMethod.POST)
	public String selectstudent(String ec,Integer teacherid,HttpSession session) {
		List<Grade> grades=gradeservice.findByTeacherId(teacherid);
		String sql="";
		if(!grades.isEmpty()) {
			for (Grade grade : grades) {
				sql+=grade.getStudent().getBj().getBjname()+"、";
			}
			if(sql.length()>9) {
				sql.substring(0, 9);
			}
			session.setAttribute("yao", "<i class='fa fa-times' aria-hidden='true' style='color: red;'></i>"
					+ "&emsp;删除失败！ 原因是"+ec+"老师在"+sql+"...等班级中任课，如需删除，请先取消"+ec+"教师的课程安排！");
			return "redirect:teacher";
		}else {
			teacherservice.deleteByID(teacherid);
			session.setAttribute("yao", "删除成功，已成功删除 "+ec);
			return "redirect:teacher";
		}
	}
	
	/***
	 * 跳转至新增修改页面
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping(value="teacherform")
	public String teacherform(Integer id,ModelMap map) {
		Teacher teachers=new Teacher();
		if(id!=null) {
			teachers = teacherservice.findById1(id);
			map.put("teacher", teachers);
			return "admin/teacherform";
		}else{
			map.put("yao", "新登记的教师密码默认为123");
			return "admin/teacherform";
		}
	}
	/***
	 * 修改新增提交
	 * @param teacher
	 * @param session
	 * @return
	 */
	@RequestMapping(value="updateadd")
	public String updateadd(Teacher teacher,HttpSession session) {
		
		if(teacher.getId()==null) {
			if(teacher.getTcname()==""||teacher.getTcuser()==""||teacher.getCourse()=="") {
				session.setAttribute("nulls", "新增失败，原因是姓名，课程，工号中的某些选项没有输入！");
				session.setAttribute("teacher", teacher);
				return "redirect:loser";
			}else {
				Teacher teacheruser=teacherservice.findByTcuser(teacher.getTcuser());
				if(teacheruser!=null) {
					session.setAttribute("nulls", "新增教师失败！原因是与"+teacheruser.getTcname()+"教师的工号（账号）重复！");
					session.setAttribute("teacher", teacher);
					return "redirect:loser";
				}else {
					session.setAttribute("update", "已成功增加教师："+teacher.getTcname()+"！");
					teacherservice.save(teacher);
					return "redirect:teacher";
				}
			}
		}else {
			if(teacher.getTcname()==""||teacher.getTcuser()==""||teacher.getCourse()=="") {
				session.setAttribute("nulls", "新增失败，原因是姓名，课程，工号中的某些选项没有输入！");
				session.setAttribute("teacher", teacher);
				return "redirect:loser";
			}else {
				Teacher teacheruser=teacherservice.findByTcuser(teacher.getTcuser());
				if(teacheruser.getId()==teacher.getId()) {
					session.setAttribute("update", teacher.getTcname()+"教师信息修改完成！");
					teacherservice.save(teacher);
					return "redirect:teacher";
				}else{
					session.setAttribute("nulls", "修改教师资料失败！原因是与"+teacheruser.getTcname()+"教师的工号（账号）重复！");
					session.setAttribute("teacher", teacher);
					return "redirect:loser";
				}
			}
		}
	}
	/***
	 * 新增/修改失败跳转
	 * @return
	 */
	@RequestMapping(value="loser")
	public String loser(HttpSession session,ModelMap map) {
		Teacher teacher = (Teacher) session.getAttribute("teacher");
		session.removeAttribute("teacher");
		map.put("teacher", teacher);
		String nulls = (String) session.getAttribute("nulls");
		map.put("nulls", nulls);
		session.removeAttribute("nulls");
		return "admin/teacherform";
	}
	
	
	
	
	
	
	
	//
	//=====================对班级表进行操作=================================================
	//
	
	/***
	 * 显示班级信息
	 * @param map
	 * @param page
	 * @return
	 */
	@RequestMapping(value="bj")
	public String bj(ModelMap map,Integer page,HttpSession session) {
		if(page==null) {
			page=0;
		}
		Pageable pageable=PageRequest.of(page, 10);
		Page<Bj> pages=bjservice.findAll(pageable);
		String sql = "<ui class='pagination pagination-sm'>";
		int a5=pages.getTotalPages();
		for (int i = 0; i < a5; i++) {
			sql+="<li><a href='bj?page="+i+"'>"+(i+1)+"</a></li>";
		}
		sql=sql+"</ui>";
		map.put("sql", sql);
		map.put("page", pages.getContent());
		map.put("xianshi","第"+(pages.getNumber()+1)+"页&emsp;显示数据"+pages.getNumberOfElements()+"条&emsp;&emsp;总共"+pages.getTotalPages()+"页&emsp;共"+pages.getTotalElements()+"条数据" );
		String hint = (String) session.getAttribute("yao");
		session.removeAttribute("yao");
		map.put("hint", hint);
		String update = (String) session.getAttribute("update");
		session.removeAttribute("update");
		map.put("update", update);
		return "admin/bj";
	}
	/***
	 * 删除 Bj(班级)表的数据
	 * @param ec
	 * @param teacherid
	 * @param session
	 * @return
	 */
	@RequestMapping(value="selectbj")
	public String selectbj(String ec,Integer bjid,HttpSession session) {
		List<Student> student=studentservice.findByBjId(bjid);
		if(!student.isEmpty()) {
			String sql="";
			for (Student st : student) {
				sql+=st.getName()+"、";
			}
			sql=sql.substring(0,sql.length()-1);
			if(sql.length()>9) {
				sql=sql.substring(0, 9);
			}
			session.setAttribute("yao","<i class='fa fa-times' aria-hidden='true' style='color: red;'></i>"
					+ "&emsp;删除失败！原因是改班级中还存在"+sql+"...等学生，如需删除，请先将"+ec+"中的学生进行处理！");
			return "redirect:bj";
		}else {
			bjservice.deleteByID(bjid);
			session.setAttribute("yao", "删除成功！"+ec+"已被删除！");
			return "redirect:bj";
		}
	}
	/***
	 * 新增 修改 班级 跳转到form
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping(value="bjform")
	public String bjform(Integer id,ModelMap map) {
		Bj bjs=new Bj();
		if(id!=null) {
			bjs=bjservice.findById1(id);
			map.put("bj", bjs);
			return "admin/bjform";
		}else {
			return "admin/bjform";
		}
	}
	
	@RequestMapping(value="bjupdateadd")
	public String bjupdateadd(Bj bj,HttpSession session) {
		if(bj.getId()==null) {
			Bj bjname=bjservice.findByBjname(bj.getBjname());
			if(bjname!=null) {
				session.setAttribute("bj", bj);
				session.setAttribute("nulls", "原因：已存在班级"+bj.getBjname()+"!");
				return "redirect:bjloser";
			}else {
				if(bj.getBjname()==null||bj.getBjteacher()==null) {
					session.setAttribute("bj", bj);
					session.setAttribute("nulls", "原因：班级或者班主任为空，不予新增班级！");
					return "redirect:bjloser";
				}else {
					session.setAttribute("update","新增"+bj.getBjname()+"成功！");
					bjservice.save(bj);
					return "redirect:bj";
				}
			}
		}else {
			Bj bjname=bjservice.findByBjname(bj.getBjname());
			if(bjname.getId()!=bj.getId()) {
				session.setAttribute("bj", bj);
				session.setAttribute("nulls", "原因：已存在班级"+bj.getBjname()+"!");
				return "redirect:bjloser";
			}else {
				if(bj.getBjname()==null||bj.getBjteacher()==null) {
					session.setAttribute("bj", bj);
					session.setAttribute("nulls", "原因：班级或者班主任为空，不予修改班级信息！");
					return "redirect:bjloser";
				}else {
					session.setAttribute("update","修改"+bj.getBjname()+"成功！");
					bjservice.save(bj);
					return "redirect:bj";
				}
			}
		}
	}
	@RequestMapping(value="bjloser")
	public String bjloser(HttpSession session,ModelMap map) {
		Bj bj = (Bj) session.getAttribute("bj");
		String nulls=(String) session.getAttribute("nulls");
		map.put("bj", bj);
		map.put("nulls", nulls);
		session.removeAttribute("bj");
		session.removeAttribute("nulls");
		return "admin/bjform";
	}
	
	//
	//========================学生管理===============================================
	//
	@RequestMapping(value="student1")
	public String student1(Integer page,Integer bjid,ModelMap map,HttpSession session) {
		if(page==null) {
			page=0;
		}
		System.out.println(bjid);
		Pageable pageable=PageRequest.of(page, 10);
		if(bjid==null||bjid==-1) {
			Page<Student> student=studentservice.findAll(pageable);
			map.put("student", student.getContent());
			map.put("xianshi","第"+(student.getNumber()+1)+"页&emsp;显示数据"+student.getNumberOfElements()+"条&emsp;&emsp;总共"+student.getTotalPages()+"页&emsp;共"+student.getTotalElements()+"条数据" );
			String sql = "<ui class='pagination pagination-sm'>";
			int a5=student.getTotalPages();
			for (int i = 0; i < a5; i++) {
				sql+="<li><a onclick='pages("+i+")'>"+(i+1)+"</a></li>";
			}
			sql=sql+"</ui>";
			map.put("sql", sql);
			List<Bj> bj=bjservice.findAll();
			map.put("bj", bj);
			map.put("pageq", page);
			map.put("bjidq", bjid);
			return "admin/student_index";
		}else{
			Page<Student> student=studentservice.findByBjId(bjid,pageable);
			map.put("bjid", bjid);
			map.put("student", student.getContent());
			map.put("xianshi","第"+(student.getNumber()+1)+"页&emsp;显示数据"+student.getNumberOfElements()+"条&emsp;&emsp;总共"+student.getTotalPages()+"页&emsp;共"+student.getTotalElements()+"条数据" );
			String sql = "<ui class='pagination pagination-sm'>";
			int a5=student.getTotalPages();
			for (int i = 0; i < a5; i++) {
				sql+="<li><a onclick='return pages1("+i+","+bjid+");'>"+(i+1)+"</a></li>";
			}
			sql=sql+"</ui>";
			map.put("sql", sql);
			List<Bj> bj=bjservice.findAll();
			map.put("bj", bj);
			map.put("pageq", page);
			map.put("bjidq", bjid);
			return "admin/student_index";
		}
	}
	
	
	@RequestMapping(value="student")
	public String student(ModelMap map,HttpSession session) {
		String  update = (String) session.getAttribute("update");
		List<Bj> bj=bjservice.findAll();
		map.put("update",update);
		map.put("bj", bj);
		return "admin/student";
	}
	@RequestMapping(value="studentdelete")
	public String studentdelete(Integer page,Integer bjid,Integer studentid,ModelMap map) {
		studentservice.deleteByID(studentid);
		map.put("sound","已成功删除当前学生");
		if(page==null) {
			page=0;
		}
		System.out.println(bjid);
		Pageable pageable=PageRequest.of(page, 10);
		if(bjid==null||bjid==-1) {
			Page<Student> student=studentservice.findAll(pageable);
			map.put("student", student.getContent());
			map.put("xianshi","第"+(student.getNumber()+1)+"页&emsp;显示数据"+student.getNumberOfElements()+"条&emsp;&emsp;总共"+student.getTotalPages()+"页&emsp;共"+student.getTotalElements()+"条数据" );
			String sql = "<ui class='pagination pagination-sm'>";
			int a5=student.getTotalPages();
			for (int i = 0; i < a5; i++) {
				sql+="<li><a onclick='pages("+i+")'>"+(i+1)+"</a></li>";
			}
			sql=sql+"</ui>";
			map.put("sql", sql);
			List<Bj> bj=bjservice.findAll();
			map.put("bj", bj);
			map.put("pageq", page);
			map.put("bjidq", bjid);
			return "admin/student_index";
		}else{
			Page<Student> student=studentservice.findByBjId(bjid,pageable);
			map.put("bjid", bjid);
			map.put("student", student.getContent());
			map.put("xianshi","第"+(student.getNumber()+1)+"页&emsp;显示数据"+student.getNumberOfElements()+"条&emsp;&emsp;总共"+student.getTotalPages()+"页&emsp;共"+student.getTotalElements()+"条数据" );
			String sql = "<ui class='pagination pagination-sm'>";
			int a5=student.getTotalPages();
			for (int i = 0; i < a5; i++) {
				sql+="<li><a onclick='return pages1("+i+","+bjid+");'>"+(i+1)+"</a></li>";
			}
			sql=sql+"</ui>";
			map.put("sql", sql);
			List<Bj> bj=bjservice.findAll();
			map.put("bj", bj);
			map.put("pageq", page);
			map.put("bjidq", bjid);
			return "admin/student_index";
		}
		
	}
	@RequestMapping(value="studentpassword")
	public String studentpassword(Integer page,Integer bjid,Integer studentid,ModelMap map) {
		studentservice.updatepassword("123", studentid);
		map.put("sound","密码重置成功，重置后密码123");
		if(page==null) {
			page=0;
		}
		System.out.println(bjid);
		Pageable pageable=PageRequest.of(page, 10);
		if(bjid==null||bjid==-1) {
			Page<Student> student=studentservice.findAll(pageable);
			map.put("student", student.getContent());
			map.put("xianshi","第"+(student.getNumber()+1)+"页&emsp;显示数据"+student.getNumberOfElements()+"条&emsp;&emsp;总共"+student.getTotalPages()+"页&emsp;共"+student.getTotalElements()+"条数据" );
			String sql = "<ui class='pagination pagination-sm'>";
			int a5=student.getTotalPages();
			for (int i = 0; i < a5; i++) {
				sql+="<li><a onclick='pages("+i+")'>"+(i+1)+"</a></li>";
			}
			sql=sql+"</ui>";
			map.put("sql", sql);
			List<Bj> bj=bjservice.findAll();
			map.put("bj", bj);
			map.put("pageq", page);
			map.put("bjidq", bjid);
			return "admin/student_index";
		}else{
			Page<Student> student=studentservice.findByBjId(bjid,pageable);
			map.put("bjid", bjid);
			map.put("student", student.getContent());
			map.put("xianshi","第"+(student.getNumber()+1)+"页&emsp;显示数据"+student.getNumberOfElements()+"条&emsp;&emsp;总共"+student.getTotalPages()+"页&emsp;共"+student.getTotalElements()+"条数据" );
			String sql = "<ui class='pagination pagination-sm'>";
			int a5=student.getTotalPages();
			for (int i = 0; i < a5; i++) {
				sql+="<li><a onclick='return pages1("+i+","+bjid+");'>"+(i+1)+"</a></li>";
			}
			sql=sql+"</ui>";
			map.put("sql", sql);
			List<Bj> bj=bjservice.findAll();
			map.put("bj", bj);
			map.put("pageq", page);
			map.put("bjidq", bjid);
			return "admin/student_index";
		}
	}
	@RequestMapping(value="xiugai")
	public String xiugai(Integer page,Integer bjid,Integer id,ModelMap map,HttpSession session) {
		session.setAttribute("pagex", page);
		session.setAttribute("bjidx", bjid);
		if(id!=null) {
			Student student=studentservice.findById(id);
			map.put("student", student);
		}
		List<Bj> bjs=bjservice.findAll();
		List<Teacher> teacher=teacherservice.findAll();
		List<Grade> grade=gradeservice.findByStudentId(id);
		
		map.put("grade", grade);
		map.put("teacher", teacher);
		map.put("bjs", bjs);
		return "admin/student_xiugai";
	}
	/**
	 * 学生新增 修改
	 * @param student
	 * @param bjId
	 * @param teacherid
	 * @return
	 */
	@RequestMapping(value="studentadd")
	public String studentadd(Student student,Integer bjId,Integer[] teacherid,HttpSession session) {
		int length = 0;
		if(teacherid!=null) {
			length = teacherid.length;
		}
		System.out.println(length);
		student.setBj(new Bj(bjId));
		studentservice.save(student);
		Grade grade=new Grade();
		List<Grade> gradesqe=gradeservice.findAll();
		Integer ids=null;
		for(Grade g:gradesqe) {
			ids=g.getId()+1;
		}
		Integer S=student.getId();
		if(teacherid!=null) {
			for(int i=0;i<length;i++) {
				grade.setStudent(new Student(S));
				grade.setId(ids);
				ids++;
				grade.setTeacher(new Teacher(teacherid[i]));
				gradeservice.save(grade);
			}
		}
		session.setAttribute("update","新增修改成功！");
		return "redirect:student";
	}
	
}

